package de.tum.in.eist.poll.shared;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * Representation of a lecture.
 */
public class Lecture implements IsSerializable {

	/**
	 * The lecture's unique ID.
	 */
	private String lectureID;

	/**
	 * The lecture's title.
	 */
	private String lectureTitle;

	/**
	 * Students who already submitted feedback.
	 */
	private Set<Student> students;

	/**
	 * Users that require notification about changes.
	 */
	private Set<User> usersToNotify;

	/**
	 * The lecture's lecturer.
	 */
	private Lecturer lecturer;

	/**
	 * States whether this Lecture is open for feedback from Students.
	 */
	private boolean openForFeeback;

	/**
	 * Current date at which the lecture is held.
	 */
	private Date currentLectureDate;

	/**
	 * Default zero-argument constructor.
	 */
	public Lecture() {
	}

	/**
	 * @param lectureID
	 *            {@link #lectureID}
	 * @param lectureTitle
	 *            {@link #lectureTitle}
	 * @param lastLectureDate
	 *            {@link #currentLectureDate}
	 * @param lecturer
	 *            {@link #lecturer}
	 */
	public Lecture(final String lectureID, final String lectureTitle,
			Date lastLectureDate, final Lecturer lecturer) {
		this.lectureID = lectureID;
		this.lectureTitle = lectureTitle;
		this.currentLectureDate = lastLectureDate;
		this.lecturer = lecturer;
		students = new HashSet<Student>();
		usersToNotify = new HashSet<User>();
		usersToNotify.add(lecturer);
	}

	/**
	 * @return the {@link #lectureID}.
	 */
	public final String getLectureID() {
		return lectureID;
	}

	/**
	 * @return the {@link #lectureTitle}.
	 */
	public final String getLectureTitle() {
		return lectureTitle;
	}

	/**
	 * @return the {@link #lecturer}.
	 */
	public final Lecturer getLecturer() {
		return lecturer;
	}

	/**
	 * @return the {@link #students}.
	 */
	protected final Set<Student> getStudents() {
		return students;
	}

	/**
	 * @param student
	 *            a student that submitted feedback
	 */
	public final void addStudent(final Student student) {
		students.add(student);
	}

	/**
	 * Checks whether a student already submitted feedback.
	 * 
	 * @param student
	 *            the student in question
	 * @return has <code>student</code> already submitted feedback for this
	 *         lecture?
	 */
	public final boolean containsStudent(final Student student) {
		for (Student containedStudent : students) {
			// name's are unique
			// -> if they are equal, it's the same student
			if (containedStudent.equals(student)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 
	 * @return Array of students who have submitted Feedback
	 */
	public Student[] getStudentsArray() {
		return students.toArray(new Student[0]);
	}

	/**
	 * Open the lecture for feedback.
	 */
	public final void openFeedback() {
		openForFeeback = true;
		currentLectureDate = new Date();
	}

	/**
	 * close the lecture for feedback.
	 */
	public final void closeFeedback() {
		students.clear();
		openForFeeback = false;
	}

	public final boolean isOpenForFeedback() {
		return openForFeeback;
	}

	/**
	 * Return the current date of the lecture.
	 * 
	 * @return current lecture date
	 */
	public final Date getCurrentLectureDate() {
		return currentLectureDate;
	}
}
